/* Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cup.plat.demo.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cup.plat.annotation.RequireAuth;
import com.cup.plat.demo.model.TenantModel;
import com.cup.plat.demo.service.TenantsService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

/**
 * @author
 */
@RestController
@Api(tags = {"Tenant"})
@RequestMapping("demo")
public class TenantDemoApi{

    @Autowired
    private TenantsService tenantsService;
    
    @ApiOperation(value = "Get all Tenant", tags = {"Tenant"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Indicates the Tenant was found and returned."),
            @ApiResponse(code = 404, message = "Indicates the requested Tenant was not found.")
    })
    @GetMapping(value = "/getall")
    @RequireAuth(roles={"11","22"})
    public List<TenantModel> getTenantAll() throws Exception {
        return tenantsService.findAll();
    }
    
    @ApiOperation(value = "Get a Tenant By TenantId", tags = {"Tenant"})
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Indicates the Tenant was found and returned."),
            @ApiResponse(code = 404, message = "Indicates the requested Tenant was not found.")
    })
    @GetMapping(value = "/get")
    public TenantModel getTenant(HttpServletRequest request, @RequestParam String tenantId) {
        return tenantsService.findByTenantId(tenantId);
    }
    
    @GetMapping(value = "/getallbyxml")
    public List<TenantModel> getallbyxml() {
        return tenantsService.getTenantByXml();
    }
    
    @PostMapping("insert")
    public int save(@RequestBody @Validated TenantModel entity) {
    	
    	return tenantsService.insert(entity);
    }
}
